using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Conusic;

namespace Imager.LastFmAutocomplete
{

    public partial class LastFmAutocompleteForm : Form
    {
        private string actualText;
        private Thread hintsThread;
        private TimeSpan timeoutToNewHints;
        public event EventHandler OnSelect;

        #region Corrent Show Window

        protected override CreateParams CreateParams
        {
            get
            {
                var s = base.CreateParams;
                s.ExStyle |= 0x08000000; // WS_EX_NOACTIVATE
                return s;
            }
        }

        private const int SW_SHOWNOACTIVATE = 4;
        private const int HWND_TOPMOST = -1;
        private const uint SWP_NOACTIVATE = 0x0010;

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        static extern bool SetWindowPos(
            int hWnd,           // window handle
            int hWndInsertAfter,    // placement-order handle
            int X,          // horizontal position
            int Y,          // vertical position
            int cx,         // width
            int cy,         // height
            uint uFlags);       // window positioning flags

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        public new void Show()
        {
            ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
            SetWindowPos(this.Handle.ToInt32(), HWND_TOPMOST,
                         this.Left, this.Top, this.Width, this.Height,
                         SWP_NOACTIVATE);
        }

        #endregion

        public LastFmAutocompleteForm()
        {
            InitializeComponent();
            this.Show();
            
           this.Hide();
        }

        public void OnTextUpdate(string text)
        {
            actualText = text;

            if (String.IsNullOrEmpty(text))
                return;

            RequestHintsAsync();
        }

        private void RequestHints()
        {
            while (timeoutToNewHints.Ticks != 0)
            {
                var oldTimeSpan = timeoutToNewHints;
                timeoutToNewHints = new TimeSpan(0);
                Thread.Sleep(oldTimeSpan);
            }

            var s = LastFm.RequestHints(actualText);


            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() => UpdateHits(s)));
            }
            else
            {
                UpdateHits(s);
            }
        }

        private void RequestHintsAsync()
        {
            timeoutToNewHints = new TimeSpan(0, 0, 0, 0, 500);
            if (hintsThread == null || !hintsThread.IsAlive)
            {
                var ths = new ThreadStart(RequestHints);
                hintsThread = new Thread(ths);
                hintsThread.Start();
            }
        }

        private void UpdateHits(List<LastFm.Hint> s)
        {
            if (s == null)
            {
                this.Hide();
                return;
            }

            flowLayoutPanel1.SuspendLayout();
            flowLayoutPanel1.Controls.Clear();
            foreach (var hint in s)
            {
                flowLayoutPanel1.Controls.Add(new AutocompleteItem(hint, OnSelect));
            }
            flowLayoutPanel1.ResumeLayout();

            if (!Visible) { Show(); }
        }

        public void MoveDown()
        {

        }

        private void LastFmAutocompleteForm_Activated(object sender, EventArgs e)
        {
        }
    }
}
